package com.zxy.service.impl;

import com.zxy.entity.Order;
import com.zxy.mapper.OrderMapper;
import com.zxy.feiclients.AccountService;
import com.zxy.service.OrderService;
import com.zxy.feiclients.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author: Zxy
 * @description:
 * @date: 2021-08-21 10:58
 */
@Service
@Slf4j
public class OrderServiceImpl implements OrderService {


    @Resource
    private OrderMapper orderMapper;

    @Resource
    private StorageService storageService;

    @Resource
    private AccountService accountService;

    @GlobalTransactional(name = "zxy_tx_group",rollbackFor = Exception.class)
    @Override
    public void create(Order order) {
        log.info("----->开始新建订单");
        orderMapper.create(order);
        log.info("----->订单微服务开始调用库存服务，做扣减");
        storageService.decrease(order.getProductId(), order.getCount());
        log.info("----->订单微服务调用库存服务完成，共扣除商品{}件",order.getCount());
        log.info("----->订单微服务开始调用账户服务，做扣减");
        accountService.decrease(order.getUserId(), order.getMoney());
        log.info("----->订单微服务调用账户服务完成，共扣除用户金额{}元",order.getMoney());

        // 修改订单状态 从0 到 1代表完成
        log.info("----->开始修改订单状态");
        orderMapper.update(order.getUserId(), 0);
        log.info("----->修改订单状态完成");
        log.info("----->下订单整个流程完成，系统无异常O(∩_∩)O哈哈~");
    }
}
